﻿using com.guojin.mj.net.message;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using com.guojin.core.io.message;
using UnityEngine;
using com.guojin.mj.net.message.game;
using UnityEngine.UI;
using DG.Tweening;
using System.Collections;
/* ***********************************************
 * Describe: 接收服务器消息
 * Author : 赵中阳
 * Email: 1148312315@qq.com
 * DATA: 2017/7/29 16:00:21 
 * FileName: ReciveMessagePage
 * Version: V1.0.1
 * ***********************************************/
public class MicReciveMessagePage : Observer
{

    private static MicReciveMessagePage instance = null;
    public static MicReciveMessagePage GetInstance()
    {
        return instance;
    }
    private void Awake()
    {
        instance = this;
    }


    private AudioClip downLoadClip;
    public bool planPlaying = false;
    private List<PlayerReceiveChatInfo> cliplist = new List<PlayerReceiveChatInfo>();

    public void OnReceiveMsg1225(string msg, Message data)
    {
        PlayerReceiveChatInfo resp = (PlayerReceiveChatInfo)data;
        switch (resp.Num) {
            case 1:
                //处理文本
                break;
            case 2:
                //处理表情
                break;
            case 3:
                //处理语音
                break;
            case 4:
                    cliplist.Add(resp);        
                break;
        }
        Debug.Log(data.toString());
    }

    private void Update() {
        StartDownLoadClip();
    }

    private void StartDownLoadClip()
    {
        if (cliplist.Count != 0 && planPlaying == false)
        {
            planPlaying = true;
            StartCoroutine(DownAddress());
        }   
    }

    IEnumerator DownAddress()
    {
        PageManager.GetInstance().ShowBan(1);
         int index  =  cliplist[0].UserIndex;
        string path = cliplist[0].ReceiveChatInfo;
        WWW www = new WWW(path);
        yield return www;
        try
        {
            downLoadClip = www.GetAudioClipCompressed(true, AudioType.OGGVORBIS);
        }
        catch (Exception)
        {
            Debug.Log("解析音频出错！");
        }
       
        cliplist.RemoveAt(0);
        PlayRecord(index, downLoadClip);
    }

    private void PlayRecord(int index, AudioClip clip)
    {
        Transform target = GameObject.Find(string.Format("loadPointGroup/player{0}/headPoint/playerHead", GameController.GetInstance().intArr[index])).transform;
        AudioSource  aud = target.GetComponent<AudioSource>();
        aud.clip = clip;
        aud.volume = 0.9f;
        aud.Play();
        planPlaying = false;
        float t = aud.clip.length;

        if (cliplist.Count == 0)
        {
            StartCoroutine(ShowBan(t));
        }

        //显示喇叭图标
        GameObject horn = new GameObject();
        horn.name = "horn";
        horn.AddComponent<Image>().sprite = Resources.Load<Sprite>("BDUI/chat/speakIcon");
        horn.transform.SetParent(target);

        Debug.Log(target);
        if (target.localPosition.x > 0) {
            horn.transform.localPosition = Vector3.zero + new Vector3(-25, -25, 0);
            horn.transform.localScale = new Vector3(-1.5f, 1.5f, 1.5f);
        } else {
            horn.transform.localPosition = Vector3.zero + new Vector3(25, 25, 0);
            horn.transform.localScale = new Vector3(1.5f, 1.5f, 1.5f);
        }

        horn.GetComponent<Image>().color = new Color(1, 1, 1, 0);
        horn.GetComponent<Image>().DOFade(1, 0.5f).SetLoops(-1, LoopType.Yoyo);
        Destroy(horn, t);
    }

    IEnumerator ShowBan(float t)
    {
        yield return new WaitForSeconds(t);
        PageManager.GetInstance().ShowBan(0);
    }
}
